package UI;

import DB.User;
import DB.userLib;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.sql.SQLException;
import java.util.Optional;


public class LogIn extends Application {
    private Stage stage;

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;
        stage.setTitle("C1科一模拟考试系统1.0");
        initImage();
        addCloseAction();
        stage.show();
    }

    private void initImage() {
        Image image = new Image("images\\img.png");
        stage.getIcons().add(image);
        GridPane parent = new GridPane();
        parent.setAlignment(Pos.CENTER);
        parent.setHgap(10);
        parent.setVgap(10);
        parent.setPadding(new Insets(25, 25, 25, 25));
        Label labelFirst = new Label("欢迎使用C1科一模拟考试系统");
        labelFirst.setStyle("-fx-text-fill: #2f465e;");
        labelFirst.setFont(Font.font("Microsoft YaHei", 24));
        parent.add(labelFirst, 0, 0, 2, 1);

        TextField userName = new TextField();
        Label userNameLabel = new Label("用户名");
        parent.add(userNameLabel, 0, 1);
        parent.add(userName, 1, 1);

        PasswordField passWard = new PasswordField();
        Label passWordLabel = new Label("密码");
        parent.add(passWordLabel, 0, 2);
        parent.add(passWard, 1, 2);

        Button logInButton = new Button("登录");
        HBox hbBtn = new HBox(10);
        hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
        hbBtn.getChildren().add(logInButton);
        parent.add(hbBtn, 1, 4);

        logInButton.setOnAction(event -> {
            //切到模拟考试场景
            try {
                User user=userLib.loginOK(userName.getText(),passWard.getText());
                if(user!=null) {
                    new Service2(stage, user);
                }else{
                    Alert alert=new Alert(Alert.AlertType.INFORMATION);
                    alert.setHeaderText("登录提示");
                    alert.setContentText("请先输入用户名和密码，要正确的");
                    alert.showAndWait();
                }
            } catch (SQLException e) {
                Alert alert=new Alert(Alert.AlertType.INFORMATION);
                alert.setHeaderText("登录失败提示");
                alert.setContentText("好吧，发生了未知错误");
                throw new RuntimeException(e);
            }

        });

       passWard.setOnKeyPressed(e->{
           if(e.getCode()==KeyCode.ENTER){
               logInButton.fire();
           }
       });

        Button deleteButton = new Button("注册");
        deleteButton.setLayoutX(200);
        deleteButton.setLayoutY(300);
        parent.add(deleteButton, 1, 4);
        deleteButton.setOnAction(e->{
            new Register(stage);

        });
        Scene scene = new Scene(parent, 400, 400);
        stage.setScene(scene);
    }

    private void addCloseAction() {
        Platform.setImplicitExit(false);
        stage.setOnCloseRequest(event -> {
            event.consume();
            Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
            alert.setTitle("请求退出程序");
            alert.setHeaderText("温馨提示");
            alert.setContentText("是否要退出C1科一模拟考试系统界面？");

            Optional<ButtonType> result = alert.showAndWait();
            if (result.get() == ButtonType.OK) {
                Platform.exit();
            }

        });
    }

}
